﻿Imports System
Imports System.Data
Imports System.Data.OleDb
Imports DTO
Imports BUS


Public Class frmLoaiVe
    Dim vtclick As Integer

    Private Sub frmLoaiVe_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        cbxTuyenXe.DataSource = TuyenBus.LayDanhSachTuyen()
        cbxTuyenXe.DisplayMember = "TenTuyen"
        cbxTuyenXe.ValueMember = "MaTuyen"

        dgvLoaiVe.DataSource = LoaiVeBus.LayDanhSachLoaiVe()
    End Sub

    Public Function LayLoaiVeTuTextboxs() As LoaiVeDto
        Dim loaive As New LoaiVeDto
        If txtMaLoaiVe.Text <> "" Then
            loaive.MaLoai = txtMaLoaiVe.Text
        End If
        loaive.MaTuyen = cbxTuyenXe.SelectedValue
        If txtGiaVe.Text <> "" Then
            loaive.GiaVe = txtGiaVe.Text
        End If

        Return loaive
    End Function

    Public Function LayLoaiVe_DataGridView(ByVal vtdong As Integer) As LoaiVeDto
        Dim loaive As New LoaiVeDto
        Try
            loaive.MaLoai = dgvLoaiVe.Rows(vtdong).Cells("MaLoaiVe").Value
            loaive.MaTuyen = dgvLoaiVe.Rows(vtdong).Cells("MaTuyen").Value
            loaive.GiaVe = dgvLoaiVe.Rows(vtdong).Cells("GiaVe").Value
        Catch ex As Exception
        End Try

        Return loaive
    End Function

    Public Sub HienThiLenTextboxs(ByVal loaive As LoaiVeDto)
        txtGiaVe.Text = loaive.GiaVe
        txtMaLoaiVe.Text = loaive.MaLoai
        cbxTuyenXe.SelectedValue = loaive.MaTuyen
    End Sub

    Public Sub ResetTextboxs()
        txtMaLoaiVe.Text = ""
        txtGiaVe.Text = ""
    End Sub

    Private Sub btnThemLoaiVe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemLoaiVe.Click
        cbxTuyenXe.Enabled = True
        ResetTextboxs()
        btnCapNhapThem.Visible = True
        btnThemLoaiVe.Visible = False
        btnHuy.Enabled = True
        btnCapNhatLoaiVe.Enabled = False
        btnXoaLoaiVe.Enabled = False
    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        cbxTuyenXe.Enabled = False
        btnThemLoaiVe.Visible = True
        btnCapNhapThem.Visible = True
        btnHuy.Enabled = False
        btnCapNhatLoaiVe.Enabled = True
        btnXoaLoaiVe.Enabled = True
    End Sub

    Private Sub btnCapNhapThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhapThem.Click

        Dim kq As Integer = LoaiVeBus.ThemLoaiVe(Me.LayLoaiVeTuTextboxs())
        If (kq = 0) Then
            MessageBox.Show("Mã loại vé đã có trong dữ liệu!!!", "THÔNG BÁO")
        Else
            dgvLoaiVe.DataSource = LoaiVeBus.LayDanhSachLoaiVe()

            dgvLoaiVe.Rows(0).Selected = False
            dgvLoaiVe.Rows(dgvLoaiVe.Rows.Count - 2).Selected = True
            Dim loaive As LoaiVeDto = LayLoaiVe_DataGridView(dgvLoaiVe.Rows.Count - 2)
            HienThiLenTextboxs(loaive)
        End If
    End Sub

    Private Sub dgvLoaiVe_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvLoaiVe.MouseClick
        vtclick = dgvLoaiVe.CurrentRow.Index
        Dim loaive As LoaiVeDto = LayLoaiVe_DataGridView(vtclick)
        HienThiLenTextboxs(loaive)
    End Sub

    Private Sub btnXoaLoaiVe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaLoaiVe.Click

        If vtclick = dgvLoaiVe.Rows.Count - 1 Then
            MessageBox.Show("Chọn dòng có dữ liệu cần xóa!!!", "THÔNG BÁO")
            Exit Sub
        End If
        Dim maCanXoa As Integer = dgvLoaiVe.Rows(vtclick).Cells("MaLoaiVe").Value
        LoaiVeBus.XoaLoaiVe(maCanXoa)
        dgvLoaiVe.DataSource = LoaiVeBus.LayDanhSachLoaiVe()
        Dim loaive As LoaiVeDto = LayLoaiVe_DataGridView(0)
        HienThiLenTextboxs(loaive)
    End Sub

    Private Sub btnCapNhatLoaiVe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhatLoaiVe.Click
        Dim loaive As LoaiVeDto = LayLoaiVeTuTextboxs()
        LoaiVeBus.CapNhatLoaiVe(loaive)
        dgvLoaiVe.DataSource = LoaiVeBus.LayDanhSachLoaiVe()

        HienThiLenTextboxs(loaive)

    End Sub
    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Close()
    End Sub

End Class